diff --git a/ChangeLog b/ChangeLog
index d3a4acf..2a39e34 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,55 @@
+2013-08-23  Herbert Xu <herbert@gondor.apana.org.au>
+
+	* Propagate EXP_QPAT in subevalvar.
+	* Initialise OPTIND after importing environment.
+	* Fixed argument parsing crash in test.
+
+2013-03-12  Peter Rosin <peda@lysator.liu.se>
+
+	* Add newline when tracing in poplocalvars.
+
+2013-01-10  Jérémie Courrèges-Anglas <jca+dash@wxcvbn.org>
+
+	* Markup fixes in manual for mandoc 1.12.1.
+
+2012-12-03  Harald van Dijk <harald@gigawatt.nl>
+
+	* Use PRIdMAX instead of %j in printf.
+
+2012-07-20  Kimo Rosenbaum <kimor79@yahoo.com>
+
+	* Fix typo for wait in manual.
+
+2012-06-09  Christoph Mathys <eraserix@gmail.com>
+
+	* Add support for ulimit -r.
+
+2012-03-11  Jim Meyering  <meyering@redhat.com>
+
+	* Avoid overflow for very long variable name.
+	$ perl -le 'print "v"x(2**31+1) ."=1"' | dash
+
+2012-02-25  Herbert Xu <herbert@gondor.apana.org.au>
+
+	* Sanitise environment variable names on entry.
+
+2011-08-17  David S. Miller <davem@davemloft.net>
+
+	* Allow building without LINEO support.
+	* Add top-level autogen.sh.
+
+2011-07-26  Harald van Dijk <harald@gigawatt.nl>
+
+	* Avoid imaxdiv when only one of the results is wanted.
+
+2010-07-09  maximilian attems <max@stro.at>
+
+	* Fix klibc DEBUG compilation.
+
+2011-07-09  Herbert Xu <herbert@gondor.apana.org.au>
+
+	* Merge SKIPFUNC/SKIPFILE and only clear SKIPFUNC when leaving dotcmd.
+
 2011-07-08  Herbert Xu <herbert@gondor.apana.org.au>
 
 	* Release 0.5.7.
--- a/configure.ac
+++ b/configure.ac
@@ -85,7 +85,7 @@ AC_CHECK_DECL([PRIdMAX],,
 ])
 
 dnl Checks for library functions.
-AC_CHECK_FUNCS(bsearch faccessat getpwnam getrlimit imaxdiv isalpha killpg \
+AC_CHECK_FUNCS(bsearch faccessat getpwnam getrlimit isalpha killpg \
 	       mempcpy \
 	       sigsetmask stpcpy strchrnul strsignal strtod strtoimax \
 	       strtoumax sysconf)
@@ -131,5 +131,10 @@ if test "$use_libedit" != "yes"; then
 else
 	export LIBS="$LIBS -ledit"
 fi
+AC_ARG_ENABLE(lineno, AS_HELP_STRING(--disable-lineno, \
+				     [Disable LINENO support]))
+if test "$enable_lineno" != "no"; then
+	AC_DEFINE([WITH_LINENO], 1, [Define if you build with -DWITH_LINENO])
+fi
 AC_CONFIG_FILES([Makefile src/Makefile])
 AC_OUTPUT
diff --git a/src/arith_yacc.c b/src/arith_yacc.c
index bf21830..1a087c3 100644
--- a/src/arith_yacc.c
+++ b/src/arith_yacc.c
@@ -94,22 +94,13 @@ static inline int higher_prec(int op1, int op2)
 
 static intmax_t do_binop(int op, intmax_t a, intmax_t b)
 {
-#ifdef HAVE_IMAXDIV
-	imaxdiv_t div;
-#endif
-
 	switch (op) {
 	default:
 	case ARITH_REM:
 	case ARITH_DIV:
 		if (!b)
 			yyerror("division by zero");
-#ifdef HAVE_IMAXDIV
-		div = imaxdiv(a, b);
-		return op == ARITH_REM ? div.rem : div.quot;
-#else
 		return op == ARITH_REM ? a % b : a / b;
-#endif
 	case ARITH_MUL:
 		return a * b;
 	case ARITH_ADD:
diff --git a/src/bltin/printf.c b/src/bltin/printf.c
index 893295c..5f9e81c 100644
--- a/src/bltin/printf.c
+++ b/src/bltin/printf.c
@@ -316,16 +316,24 @@ out:
 static char *
 mklong(const char *str, const char *ch)
 {
+	/*
+	 * Replace a string like "%92.3u" with "%92.3"PRIuMAX.
+	 *
+	 * Although C99 does not guarantee it, we assume PRIiMAX,
+	 * PRIoMAX, PRIuMAX, PRIxMAX, and PRIXMAX are all the same
+	 * as PRIdMAX with the final 'd' replaced by the corresponding
+	 * character.
+	 */
+
 	char *copy;
 	size_t len;	
 
-	len = ch - str + 3;
+	len = ch - str + sizeof(PRIdMAX);
 	STARTSTACKSTR(copy);
 	copy = makestrspace(len, copy);
-	memcpy(copy, str, len - 3);
-	copy[len - 3] = 'j';
+	memcpy(copy, str, len - sizeof(PRIdMAX));
+	memcpy(copy + len - sizeof(PRIdMAX), PRIdMAX, sizeof(PRIdMAX));
 	copy[len - 2] = *ch;
-	copy[len - 1] = '\0';
 	return (copy);	
 }
 
diff --git a/src/bltin/test.c b/src/bltin/test.c
index 90135e1..baa91a5 100644
--- a/src/bltin/test.c
+++ b/src/bltin/test.c
@@ -268,9 +268,13 @@ aexpr(enum token n)
 static int
 nexpr(enum token n)
 {
-	if (n == UNOT)
-		return !nexpr(t_lex(++t_wp));
-	return primary(n);
+	if (n != UNOT)
+		return primary(n);
+
+	n = t_lex(t_wp + 1);
+	if (n != EOI)
+		t_wp++;
+	return !nexpr(n);
 }
 
 static int
diff --git a/src/dash.1 b/src/dash.1
index 3847d98..6241a61 100644
--- a/src/dash.1
+++ b/src/dash.1
@@ -1715,7 +1715,7 @@ The
 .Ar argument
 is printed in the style
 .Sm off
-.Pf [\-]d Cm \&. No ddd Cm e No \\*(Pmdd
+.Pf [\-]d Cm \&. No ddd Cm e No \*(Pmdd
 .Sm on
 where there
 is one digit before the decimal point and the number after is equal to
@@ -2134,9 +2134,9 @@ is specified, the soft limit is displayed or both limits are set.
 If both are specified, the last one wins.
 .El
 .Pp
-.Bl -tag -width Fl
 The limit to be interrogated or set, then, is chosen by specifying
 any one of these flags:
+.Bl -tag -width Fl
 .It Fl a
 show all the current limits
 .It Fl t
@@ -2163,6 +2163,8 @@ show or set the limit on the number of processes this user can
 have at one time
 .It Fl n
 show or set the limit on the number files a process can have open at once
+.It Fl r
+show or set the limit on the real-time scheduling priority of a process
 .El
 .Pp
 If none of these is specified, it is the limit on file size that is shown
@@ -2205,7 +2207,7 @@ options are given, only the variable is unset.
 Wait for the specified job to complete and return the exit status of the
 last process in the job.
 If the argument is omitted, wait for all jobs to
-complete and the return an exit status of zero.
+complete and return an exit status of zero.
 .El
 .Ss Command Line Editing
 When
diff --git a/src/eval.c b/src/eval.c
index 95d30f4..c7358a6 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -1039,7 +1039,7 @@ returncmd(int argc, char **argv)
 	 * If called outside a function, do what ksh does;
 	 * skip the rest of the file.
 	 */
-	evalskip = funcline ? SKIPFUNC : SKIPFILE;
+	evalskip = SKIPFUNC;
 	return argv[1] ? number(argv[1]) : exitstatus;
 }
 
diff --git a/src/eval.h b/src/eval.h
index 4e4de30..dc8acd2 100644
--- a/src/eval.h
+++ b/src/eval.h
@@ -61,4 +61,3 @@ extern int evalskip;
 #define SKIPBREAK	(1 << 0)
 #define SKIPCONT	(1 << 1)
 #define SKIPFUNC	(1 << 2)
-#define SKIPFILE	(1 << 3)
diff --git a/src/expand.c b/src/expand.c
index ce60fe9..11fd7b7 100644
--- a/src/expand.c
+++ b/src/expand.c
@@ -650,7 +650,8 @@ subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varfla
 	char *(*scan)(char *, char *, char *, char *, int , int);
 
 	argstr(p, EXP_TILDE | (subtype != VSASSIGN && subtype != VSQUESTION ?
-			       (flag & EXP_QUOTED ? EXP_QPAT : EXP_CASE) : 0));
+			       (flag & (EXP_QUOTED | EXP_QPAT) ?
+			        EXP_QPAT : EXP_CASE) : 0));
 	STPUTC('\0', expdest);
 	argbackq = saveargbackq;
 	startp = stackblock() + startloc;
diff --git a/src/main.c b/src/main.c
index af987c6..f79ad7d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -242,7 +242,7 @@ cmdloop(int top)
 
 		skip = evalskip;
 		if (skip) {
-			evalskip = 0;
+			evalskip &= ~SKIPFUNC;
 			break;
 		}
 	}
diff --git a/src/miscbltin.c b/src/miscbltin.c
index e354df4..b596fd2 100644
--- a/src/miscbltin.c
+++ b/src/miscbltin.c
@@ -392,6 +392,9 @@ static const struct limits limits[] = {
 #ifdef RLIMIT_LOCKS
 	{ "locks",			RLIMIT_LOCKS,	   1, 'w' },
 #endif
+#ifdef RLIMIT_RTPRIO
+	{ "rtprio",			RLIMIT_RTPRIO,	   1, 'r' },
+#endif
 	{ (char *) 0,			0,		   0,  '\0' }
 };
 
diff --git a/src/parser.c b/src/parser.c
index 6de2762..572cbcd 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -853,7 +853,7 @@ readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
 {
 	int c = firstc;
 	char *out;
-	int len;
+	size_t len;
 	struct nodelist *bqlist;
 	int quotef;
 	int dblquote;
diff --git a/src/show.c b/src/show.c
index 14dbef3..4a049e9 100644
--- a/src/show.c
+++ b/src/show.c
@@ -378,7 +378,11 @@ opentrace(void)
 	scopy("./trace", s);
 #endif /* not_this_way */
 	if (tracefile) {
+#ifndef __KLIBC__
 		if (!freopen(s, "a", tracefile)) {
+#else
+		if (!(!fclose(tracefile) && (tracefile = fopen(s, "a")))) {
+#endif /* __KLIBC__ */
 			fprintf(stderr, "Can't re-open %s\n", s);
 			debug = 0;
 			return;
@@ -394,7 +398,9 @@ opentrace(void)
 	if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
 		fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
 #endif
+#ifndef __KLIBC__
 	setlinebuf(tracefile);
+#endif /* __KLIBC__ */
 	fputs("\nTracing started.\n", tracefile);
 }
 #endif /* DEBUG */
diff --git a/src/var.c b/src/var.c
index ecc8c90..fa24854 100644
--- a/src/var.c
+++ b/src/var.c
@@ -101,7 +101,9 @@ struct var varinit[] = {
 	{ 0,	VSTRFIXED|VTEXTFIXED,		"PS2=> ",	0 },
 	{ 0,	VSTRFIXED|VTEXTFIXED,		"PS4=+ ",	0 },
 	{ 0,	VSTRFIXED|VTEXTFIXED,		"OPTIND=1",	getoptsreset },
+#ifdef WITH_LINENO
 	{ 0,	VSTRFIXED|VTEXTFIXED,		linenovar,	0 },
+#endif
 #ifndef SMALL
 	{ 0,	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM\0",	0 },
 	{ 0,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE\0",	sethistsize },
@@ -134,11 +136,14 @@ INIT {
 
 	initvar();
 	for (envp = environ ; *envp ; envp++) {
-		if (strchr(*envp, '=')) {
+		p = endofname(*envp);
+		if (p != *envp && *p == '=') {
 			setvareq(*envp, VEXPORT|VTEXTFIXED);
 		}
 	}
 
+	setvarint("OPTIND", 1, 0);
+
 	fmtstr(ppid + 5, sizeof(ppid) - 5, "%ld", (long) getppid());
 	setvareq(ppid, VTEXTFIXED);
 
@@ -335,9 +340,11 @@ lookupvar(const char *name)
 	struct var *v;
 
 	if ((v = *findvar(hashvar(name), name)) && !(v->flags & VUNSET)) {
+#ifdef WITH_LINENO
 		if (v == &vlineno && v->text == linenovar) {
 			fmtstr(linenovar+7, sizeof(linenovar)-7, "%d", lineno);
 		}
+#endif
 		return strchrnul(v->text, '=') + 1;
 	}
 	return NULL;
@@ -542,7 +549,7 @@ poplocalvars(int keep)
 	while ((lvp = next) != NULL) {
 		next = lvp->next;
 		vp = lvp->vp;
-		TRACE(("poplocalvar %s", vp ? vp->text : "-"));
+		TRACE(("poplocalvar %s\n", vp ? vp->text : "-"));
 		if (keep) {
 			int bits = VSTRFIXED;
 
diff --git a/src/var.h b/src/var.h
index 54f7b2d..79ee71a 100644
--- a/src/var.h
+++ b/src/var.h
@@ -88,9 +88,15 @@ extern struct var varinit[];
 #define vps2 (&vps1)[1]
 #define vps4 (&vps2)[1]
 #define voptind (&vps4)[1]
+#ifdef WITH_LINENO
 #define vlineno (&voptind)[1]
+#endif
 #ifndef SMALL
+#ifdef WITH_LINENO
 #define vterm (&vlineno)[1]
+#else
+#define vterm (&voptind)[1]
+#endif
 #define vhistsize (&vterm)[1]
 #endif
 
-- 
1.8.4

